package mess;

public class LeeCode_7 {
    public static void main(String[] args) {
        System.out.println(reverse(-2147483648));
    }
    private static int reverse(int x) {
        long tempX;
        if (x < 0)
            tempX = -x;
        else
            tempX = x;
        if (x == -2147483648){
            return 0;
        }
        long num = tempX;
        long result = 0;
        long temp = 1;
        while (num >= 10){
            num /= 10;
            temp *= 10;
        }
        num = tempX;
        while (num != 0){
            long single = num % 10;
            result += single * temp;
            temp /= 10;
            num /= 10;
        }
        if ((result > 2147483647 && x > 0) || (-result < -2147483648 && x < 0)){
            return 0;
        }
        if (x < 0)
            return -(int) result;
        return (int) result;
    }
}
